package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @GetMapping("/page")
    public Result getPage(SetmealPageQueryDTO queryDTO) {
        PageResult pageResult = new PageResult();
        pageResult = setMealService.getPage(queryDTO);
        return Result.success(pageResult);
    }

    @CacheEvict(value = "setmealCache",key = "#setmealDTO.categoryId")
    @PostMapping
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        setMealService.add(setmealDTO);
        return Result.success();
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,long id) {
        setMealService.updateStatus(status,id);
        return Result.success();
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setMealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public Result delete(long[] ids) {
        //如果数组为空。抛出异常
        if (ids == null || ids.length == 0) {
            return Result.error("请选择要删除的套餐");
        }
        setMealService.delete(ids);
        return Result.success();
    }
}
